import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class ListViewHomePage extends StatefulWidget {
  @override
  _ListViewHomePageState createState() => _ListViewHomePageState();
}

class _ListViewHomePageState extends State<ListViewHomePage> {
  List list = [];
  RefreshController controller = new RefreshController();
  Widget defaultHeader = defaultTargetPlatform == TargetPlatform.iOS
      ? ClassicHeader(
          idleText: "下拉刷新",
          releaseText: "释放刷新",
          refreshingText: "刷新中",
          failedText: "刷新失败",
          completeText: "刷新成功")
      : MaterialClassicHeader();

  final LoadIndicator defaultFooter = ClassicFooter(
      idleText: "上拉加载",
      failedText: "加载失败",
      loadingText: "加载中...",
      noDataText: "没有更多啦",
      canLoadingText: "松开加载");

  @override
  void initState() {
    super.initState();
    for (int i = 0; i < 20; i++) {
      list.add("list $i");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("listview配置"),
      ),

      // 线性布局  构建分割线
      body: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: defaultHeader,
        footer: defaultFooter,
        child: buildListView(),
        controller: controller,
        onRefresh: () {
          print("下拉刷新回调");
          Future.delayed(new Duration(milliseconds: 3000), () {
            controller.refreshCompleted();
          });
        },
        onLoading: () {
          print("上拉加载回调");
          Future.delayed(new Duration(milliseconds: 3000), () {
            controller.loadComplete();
          });
        },
      ),
    );
  }

  // 构建list列表
  Widget buildListView() {
    return ListView.builder(
      physics: BouncingScrollPhysics(),
      itemBuilder: (BuildContext context, num index) {
        return Container(
            margin: EdgeInsets.all(5),
            padding: EdgeInsets.all(5),
            alignment: Alignment.center,
            width: 80,
            height: 80,
            child: Text(list[index]),
            color: Colors.grey);
      },
      itemCount: list.length,
    );
  }
}
